<?php

namespace app\admin\model;

use app\common\model\TimeModel;

/**
 * Class UserLevel
 * @package app\admin\model
 * User : Orange Tel: 181-3414-1353 QQ:1139552211
 * Date    : 2021/6/9 上午11:50
 */
class UserLevel extends TimeModel {

    protected $name = "user_level";

    protected $deleteTime = "delete_time";


    /**
     *获取指定升级条件
     * @param $id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * User    : QingYang
     * Tel     : 181-3414-1353
     * QQ      : 1139552211
     * Date    : 2021/6/2 上午10:04
     */
    public function getUserLevelFind($id) {
        return self::instance()->where(['id' => $id])->field([
            'id',
            'level',
            'invite',
            'buy_num',
            'brokerage_sum'
        ])->find()->toArray();
    }

    /**
     * 分批处理用户升级
     * @param $user_items
     * @param bool $type 是否为单个用户
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException User    : QingYang
     * Tel     : 181-3414-1353
     * QQ      : 1139552211
     * Date    : 2021/6/8 下午12:12
     */
    public function userLevel($user_items, $type = false) {
        $i           = 1;
        $level_items = config('leveltype')['level'];
        $l_len       = count($level_items);
        foreach ($level_items as $l_itemKey => $l_item) {
            //获取升级条件
            $condition   = UserLevel::instance()->getUserLevelFind($l_itemKey);
            $p_condition = false;
            //判断是否为最后一级
            if ($i !== $l_len) {
                $p_condition = UserLevel::instance()->getUserLevelFind($l_itemKey + 1);
            }
            //升级
            UserLevel::instance()->userLevelUp($user_items, $condition, $p_condition, $type);

            $i++;
        }
    }

    /**
     * 升级用户分批处理
     * @param $user_items
     * @param $condition
     * @param $p_condition
     * @param bool $type
     * @return bool
     * User    : QingYang
     * Tel     : 181-3414-1353
     * QQ      : 1139552211
     * Date    : 2021/6/2 上午11:23
     */
    public function userLevelUp($user_items, $condition, $p_condition, $type = false) {
        //判断是否需要分配处理升级
        if (empty($type)) {
            if (array_level($user_items) > 2) {
                for ($i = 0; $i < count($user_items); $i++) {
                    $this->moveUp($user_items[$i], $condition, $p_condition);
                }
            } else {
                $this->moveUp($user_items, $condition, $p_condition);
            }
        } else {
            $this->moveUp($user_items, $condition, $p_condition, $type);

        }


        return true;

    }

    /**
     * 判断是否符合升级条件
     * @param $user_info
     * @param $condition
     * @param $p_condition
     * @return array|bool
     * User    : QingYang
     * Tel     : 181-3414-1353
     * QQ      : 1139552211
     * Date    : 2021/6/2 上午11:10
     */
    public function ifLevelUp($user_info, $condition, $p_condition) {
        //获取邀请人数
        $invite_count = User::instance()->where(['superior' => $user_info['id']])->count();
        //获取购买数量
        $buy_num = Order::instance()->where(['uid' => $user_info['id'], 'status' => ['!=', '1,10']])->count();
        //佣金积累
        $brokerage_sum = UserAmountLog::instance()->where([
            'user_id' => $user_info['id'],
            'type'    => 6
        ])->sum('amount');
        //判断是否为最高等级提升
        if ($p_condition !== false) {
            //符合条件则升级、不符合则不升级、若后期条件不满足则自动降级
            if (($invite_count >= $condition['invite'] && $invite_count < $p_condition['invite']) && ($buy_num >= $condition['buy_num'] && $buy_num < $p_condition['buy_num']) && ($brokerage_sum >= $condition['brokerage_sum'] && $brokerage_sum < $p_condition['brokerage_sum'])) {
                //重新赋值等级
                $user_info['level_id'] = $condition['id'];
                return $user_info;
            }
        }
        //符合条件则升级、不符合则不升级、若后期条件不满足则自动降级
        if ($invite_count >= $condition['invite'] && $buy_num >= $condition['buy_num'] && $brokerage_sum >= $condition['brokerage_sum']) {
            //重新赋值等级
            $user_info['level_id'] = $condition['id'];
            return $user_info;
        }
        return false;
    }

    /**
     *升级
     * @param $user_items
     * @param $condition
     * @param $p_condition
     * @param bool $type
     * User    : QingYang
     * Tel     : 181-3414-1353
     * QQ      : 1139552211
     * Date    : 2021/6/8 下午3:44
     */
    public function moveUp($user_items, $condition, $p_condition, $type = false) {
        if (empty($type)) {
            foreach ($user_items as $u_itemKey => $u_item) {
                //达成升级or降级条件则重新赋值，未达到则去除
                $user_info = $this->ifLevelUp($user_items[$u_itemKey], $condition, $p_condition);
                if (!empty($user_info)) {
                    User::instance()->where(['id' => $u_item['id']])->update([
                        'level_id'    => $user_info['level_id'],
                        'update_time' => time()
                    ]);
                }
            }
        } else {
            //达成升级or降级条件则重新赋值，未达到则去除
            $user_info = $this->ifLevelUp($user_items, $condition, $p_condition);
            if (!empty($user_info)) {
                User::instance()->where(['id' => $user_items['id']])->update([
                    'level_id'    => $user_info['level_id'],
                    'update_time' => time()
                ]);
            }
        }
    }


}